import { fileURLToPath, URL } from "node:url";
import type { ConfigEnv, UserConfig } from "vite";
import { defineConfig } from "vite";
import { pluginExposeRenderer } from "./vite.base.config";
import vue from "@vitejs/plugin-vue";
import path from "path";
import AutoImport from "unplugin-auto-import/vite";
import legacy from "@vitejs/plugin-legacy";
import autoprefixer from "autoprefixer";
import Components from "unplugin-vue-components/vite";
import { AntDesignVueResolver } from "unplugin-vue-components/resolvers";
// https://vitejs.dev/config
// 将源码打包进exe
export default defineConfig((env) => {
    const forgeEnv = env as ConfigEnv<"renderer">;
    const { root, mode, forgeConfigSelf } = forgeEnv;
    const name = forgeConfigSelf.name ?? "";

    return {
        root: path.join(root, "..", "electron-renderer"),
        mode,
        base: "./",
        css: {
            preprocessorOptions: {
                scss: {
                    additionalData: `@import "@/styles/_index.scss";`,
                },
            },
            postcss: {
                plugins: [autoprefixer()],
            },
        },
        build: {
            // outDir: `.vite/renderer/${name}`,
            outDir: path.join(root, `.vite/renderer/${name}`),
            target: "es2015",
            assetsDir: "static",
            minify: "esbuild",
            // terserOptions: {
            //     compress: {
            //         // drop_console: true,
            //         drop_debugger: true,
            //     },
            // },
            rollupOptions: {},
        },
        plugins: [
            pluginExposeRenderer(name),
            vue(),
            Components({
                resolvers: [AntDesignVueResolver({ importStyle: false })],
            }),
            AutoImport({
                include: [
                    /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
                    /\.vue$/,
                    /\.vue\?vue/, // .vue
                    /\.md$/, // .md
                ],
                imports: ["vue", "vue-router", "pinia"],
                dts: "./auto-imports.d.ts",
                eslintrc: {
                    enabled: false, // Default `false`
                    filepath: "./.eslintrc-auto-import.json", // Default `./.eslintrc-auto-import.json`
                    globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable'),
                },
            }),
            legacy({
                targets: ["ie >= 11"],
                additionalLegacyPolyfills: ["regenerator-runtime/runtime"],
            }),
        ],
        resolve: {
            preserveSymlinks: true,
            alias: {
                "@": path.join(root, "..", "electron-renderer/src"),
            },
        },

        clearScreen: false,
    } as UserConfig;
});

// export default defineConfig((env) => {
//     const forgeEnv = env as ConfigEnv<"renderer">;
//     const { root, mode, forgeConfigSelf } = forgeEnv;
//     const name = forgeConfigSelf.name ?? "";

//     return {
//         root,
//         mode,
//         base: "./",
//         build: {
//             outDir: `.vite/renderer/${name}`,
//         },
//         plugins: [pluginExposeRenderer(name)],
//         resolve: {
//             preserveSymlinks: true,
//         },
//         clearScreen: false,
//     } as UserConfig;
// });
